/*! ******************************************************************************
 *
 * Created on 2022年3月29日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.yzbdl.lanius.ui.spoon.middle.ext.progress;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ProgressBar;
import org.pentaho.di.ui.core.PropsUI;

/**
 * @author hs@yzbdl.ac.cn
 *
 */
public class ProgressBarUnit extends Composite {

    private String name;
    private ProgressBar progressBar;

    public ProgressBarUnit(Composite parent, Progress progress) {
        this(parent, SWT.NONE, progress);
        PropsUI.getInstance().setLook(this);
    }

    /**
     * @param parent
     * @param style
     */
    private ProgressBarUnit(Composite parent, int style, Progress progress) {
        super(parent, style);
        this.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        GridLayout gridLayout = new GridLayout(1, true);
        this.setLayout(gridLayout);

        name = progress.getName();


        progressBar = new ProgressBar(this, SWT.HORIZONTAL | SWT.SMOOTH);
        progressBar.setMinimum(0);
        progressBar.setMaximum(progress.getMaximum());
        progressBar.setSelection(progress.getSelection());
        GridData barData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
        progressBar.setLayoutData(barData);
    }

    /**
     * 刷新UI
     */
    public void refreshUI(Progress progress) {
        this.progressBar.setSelection(progress.getSelection());
    }

    @Override
    public void dispose() {
        if (progressBar != null && !progressBar.isDisposed()) {
            progressBar.dispose();
        }
        super.dispose();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProgressBarUnit other = (ProgressBarUnit)obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}
